package com.example.demo.listener;

import java.util.Hashtable;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

//@WebListener
//public class SingliLoginListener implements HttpSessionListener, HttpSessionAttributeListener,ServletContextListener{
//private Hashtable<String,HttpSession>  sessions=new Hashtable<String,HttpSession>();
///**
// * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent)
// */
//public void sessionDestroyed(HttpSessionEvent arg0)  { 
//	//获取登录账号
//	Object o = arg0.getSession().getAttribute("account");
//	if(o!=null){
//		String account=(String) o;
//		sessions.remove(account);
//	}
//	onlionNum(arg0,-1);
//}
//
///**
// * @see HttpSessionAttributeListener#attributeAdded(HttpSessionBindingEvent)
// */
//public void attributeAdded(HttpSessionBindingEvent se)  { 
//	//添加或替换操作的键
//	String key=se.getName();
//	if(key.equals("account")){
//		//登录成功时触发的监听事件
//		//获取当前登录的session
//		HttpSession currentSession = se.getSession();
//		//获取登录账号
//		String account=(String) currentSession.getAttribute(key);
//		//检查 用户的账号是否已经在其他主机上登录
//		HttpSession oldSession=sessions.get(account);
//		if(oldSession!=null && !oldSession.getId().equals(currentSession.getId())){
//			//当前账号已经在其他主机上登录过
//			//将之前的登录状态取消
//			oldSession.removeAttribute("account");
//		}
//		//将当前登录信息存起来
//		sessions.put(account, currentSession);
//	}
//}
//
///**
// * @see HttpSessionAttributeListener#attributeRemoved(HttpSessionBindingEvent)
// */
//public void attributeRemoved(HttpSessionBindingEvent arg0)  { 
//    
//}
//
///**
// * @see HttpSessionAttributeListener#attributeReplaced(HttpSessionBindingEvent)
// */
//public void attributeReplaced(HttpSessionBindingEvent arg0)  { 
//	attributeAdded(arg0);
//}
//
//@Override
//public void sessionCreated(HttpSessionEvent arg0) {
//	onlionNum(arg0,1);
//	
//}
//
//@Override
//public void contextDestroyed(ServletContextEvent arg0) {
//	// TODO Auto-generated method stub
//	
//}
//
//@Override
//public void contextInitialized(ServletContextEvent arg0) {
//	//监听事件获取application对象
//	ServletContext application=arg0.getServletContext();
//	//设置初始在线人数
//	application.setAttribute("onlineNum", 3000);
////	System.out.println(application.getAttribute("onlineNum"));
//}
//public synchronized void onlionNum(HttpSessionEvent se,int number){
//	ServletContext application = se.getSession().getServletContext();
//	Object o = application.getAttribute("onlineNum");
//	if(o!=null){
//		int numbers=(int)o;
//		numbers+=number;
//		System.out.println(numbers);
//		application.setAttribute("onlineNum", numbers);
//	}
//}
//}
